package com.xbongbong.paas.elasticsearch.model;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.PaasFormSubDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.elasticsearch.pojo.QueryListVO;
import com.xbongbong.paas.elasticsearch.pojo.XbbAggregatedPage;
import com.xbongbong.paas.elasticsearch.pojo.dto.DeleteDTO;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.pojo.dto.PhysicalDeleteDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeEsAddDTO;
import com.xbongbong.saas.domain.entity.UpdateStockDataEntity;
import com.xbongbong.saas.domain.entity.WorkOrderEntity;
import com.xbongbong.saas.domain.entity.ext.CustomerCommunicateEntityExt;
import com.xbongbong.saas.domain.entity.ext.PayPlanSheetEntityExt;
import com.xbongbong.saas.domain.entity.ext.PaymentSheetEntityExt;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * ES操作通用方法
 * @author 梁鲁江
 * @since
 * @version
 */
public interface PaasEsModel {

    /**
     * 插入
     * @param insertDTO
     * @return
     * @throws XbbException
     */
    void insert(InsertDTO insertDTO) throws XbbException;

    /**
     * 批量插入
     * @param insertList
     * @throws XbbException
     */
    void insertBatch(List<InsertDTO> insertList) throws XbbException  ;

    /**
     * 批量插入需要传入刷新策略
     * @param insertList
     * @param refreshPolicy
     * @throws XbbException
     */
    void insertBatch(List<InsertDTO> insertList, WriteRequest.RefreshPolicy refreshPolicy)  throws XbbException  ;

    /**
     * 批量插入需要传入是否报错是否抛出异常
     * @param insertList
     * @param refreshPolicy
     * @param needThrow
     * @throws XbbException
     */
    void insertBatch(List<InsertDTO> insertList, WriteRequest.RefreshPolicy refreshPolicy,boolean needThrow)  throws XbbException  ;

    /**
     * 批量插入数据，是要默认塞入my_join_field
     * @param insertList
     * @param refreshPolicy
     * @param needThrow
     * @throws XbbException
     */
    void insertBatch(List<InsertDTO> insertList, WriteRequest.RefreshPolicy refreshPolicy,boolean needThrow,boolean needJoinField)  throws XbbException  ;


    /**
     * 业务代码禁止使用！！！
     * 异步批量插入
     * @param insertList
     * @param refreshPolicy
     * @param needThrow
     * @throws XbbException
     */
    @Deprecated
    void insertBatchAsync(List<InsertDTO> insertList, WriteRequest.RefreshPolicy refreshPolicy,boolean needThrow)  throws XbbException  ;

    /**
     * 更新
     * @param insertDTO
     * @throws XbbException
     */
    void update(InsertDTO insertDTO) throws XbbException;

    /**
     * 批量更新
     * @param insertList
     * @throws XbbException
     */
    void updateBatch(List<InsertDTO> insertList) throws XbbException;

    void updateBatch(List<InsertDTO> insertList, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;

    void updateBatch(List<InsertDTO> insertList, WriteRequest.RefreshPolicy refreshPolicy,boolean needThrow) throws XbbException;
    /**
     * 批量删除
     * @param list id列表
     * @param corpid  公司id
     * @param del
     * @see com.xbongbong.paas.enums.DelEnum
     * @param indexTypeEnum
     * @see IndexTypeEnum
     * @throws XbbException
     */
    void deleteBatch(List<Long> list, String corpid, Integer del, IndexTypeEnum indexTypeEnum) throws XbbException;

    /**
     * 批量删除
     * @param list id列表
     * @param corpid  公司id
     * @param del
     * @see com.xbongbong.paas.enums.DelEnum
     * @param indexTypeEnum
     * @see IndexTypeEnum
     * @throws XbbException
     */
    void deleteBatch(List<Long> list, String corpid, Integer del, IndexTypeEnum indexTypeEnum, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;

    /**
     * Description: 批量物理删除
     * @param list id列表
     * @param corpid 公司id
     * @param indexTypeEnum
     * @author 魏荣杰
     * @date 2019/5/23 11:30
     * @since v1.0
     */
    void physicalDeleteBatch(List<Long> list, String corpid, IndexTypeEnum indexTypeEnum) throws XbbException;
    void physicalDeleteBatchChild(List<DeleteDTO> list, String corpid, IndexTypeEnum indexTypeEnum) throws XbbException;

    /**
     * Description: 物理删除子表单数据
     * @param corpid
     * @param subDataEntities
     * @param indexTypeEnum
     * @return void
     * @throws
     * @author 魏荣杰
     * @date 2020/9/30 15:35
     * @since
     */
    void physicalDeleteSubBatch(String corpid, List<? extends PaasFormSubDataEntity> subDataEntities, IndexTypeEnum indexTypeEnum) throws XbbException;

    /**
     * 批量更新
     * @param list 数据列表 格式：[{"corpid": "", "id": "", "data":[{"key": "$.text_1", "esKey": "data.text_1", "value": ""}]}]
     * @param indexTypeEnum
     * @see IndexTypeEnum
     * @throws XbbException
     */
    void updateBatchMuchField(List<UpdateDataEntity> list, IndexTypeEnum  indexTypeEnum) throws XbbException;

    void updateBatchMuchField(List<UpdateDataEntity> list, IndexTypeEnum  indexTypeEnum,boolean needThrow) throws XbbException;

    void updateBatchMuchField(List<UpdateDataEntity> list, IndexTypeEnum  indexTypeEnum, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;

    /**
     * 批量更新
     * @param updateDataEntity 数据 {"corpid": "", "id": "", "data":[{"key": "$.text_1", "esKey": "data.text_1", "value": ""}]}
     * @param indexTypeEnum
     * @see IndexTypeEnum
     * @throws XbbException
     */
    void updateBySingle(UpdateDataEntity updateDataEntity, IndexTypeEnum  indexTypeEnum) throws XbbException;

    /**
     * 删除（del=1，后面可以扩展del的变量）
     * @param deleteDTO
     * @throws XbbException
     */
    void delete(DeleteDTO deleteDTO) throws XbbException;

    /**
     * 删除，del变量传入，可根据需求指定del值
     * @param deleteDTO
     * @throws XbbException
     */
    void delete(DeleteDTO deleteDTO, DelEnum delEnum) throws XbbException;

    /**
     * 获取es插入实体
     * @param paasFormDataEntity 数据实体
     * @param indexTypeEnum es索引
     * @return es插入实体
     * @throws XbbException 拷贝属性异常
     */
    InsertDTO getSaveDTO(PaasFormDataEntity paasFormDataEntity, IndexTypeEnum indexTypeEnum) throws XbbException;

    /**
     * 获取跟进记录es插入实体
     * @param customerCommunicateEntityExt 数据实体
     * @param indexTypeEnum es索引
     * @return com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO es插入实体
     * @throws XbbException 拷贝属性异常
     * @author zcp
     * @date 2019/6/15 22:14
     * @since v1.0
     * @version v1.0
     */
    InsertDTO getSaveDTO(CustomerCommunicateEntityExt customerCommunicateEntityExt, IndexTypeEnum indexTypeEnum) throws XbbException;

    /**
     * Description: 获取es插入实体(工单用)
     * @param workOrderEntity 工单实体
     * @param indexTypeEnum es索引
     * @return com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO
     * @throws XbbException XbbException
     * @author 魏荣杰
     * @date 2019/4/16 11:26
     * @since v1.0
     */
    InsertDTO getSaveDTO(WorkOrderEntity workOrderEntity, IndexTypeEnum indexTypeEnum) throws XbbException;

    /**
     * 获取回款单es插入实体
     * @param paymentSheetEntityExt 回款单实体
     * @param indexTypeEnum 索引枚举
     * @return com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO
     * @throws XbbException
     * @author zcp
     * @date 19/11/18 018 14:20
     * @update 19/11/18 018 14:20
     * @since v1.0
     * @version v1.0
     */
    InsertDTO getSaveDTO(PaymentSheetEntityExt paymentSheetEntityExt, IndexTypeEnum indexTypeEnum) throws XbbException;

    /**
     * 获取付款单es插入实体
     * @param payPlanSheetEntityExt 付款单实体
     * @param indexTypeEnum 索引枚举
     * @return com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO
     * @throws XbbException
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    InsertDTO getSaveDTO(PayPlanSheetEntityExt payPlanSheetEntityExt, IndexTypeEnum indexTypeEnum) throws XbbException;

    InsertDTO getSaveDTO(PaasFormSubDataEntity paasFormSubDataEntity, IndexTypeEnum indexTypeEnum) throws XbbException;

    /**
     * Description: 获取es插入实体(工单用)
     * @param indexTypeEnum es索引
     * @return com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO
     * @throws XbbException XbbException
     * @author 魏荣杰
     * @date 2019/4/16 11:26
     * @since v1.0
     */
    InsertDTO getSaveDTO(KnowledgeEsAddDTO knowledgeEsAddDTO, IndexTypeEnum indexTypeEnum) throws XbbException;

    /**
     * 插入
     * @param entity 保存实体
     * @param indexTypeEnum es索引
     * @param corpid 公司id
     * @param id 主体id
     * @param parentId 父id，可为空
     * @return
     */
    <T> void insert(T entity, IndexTypeEnum indexTypeEnum, String corpid, Long id, Long parentId) throws XbbException;

    /**
     * 插入
     * @param entity 保存实体
     * @param indexTypeEnum es索引
     * @param corpid 公司id
     * @param id 主体id
     * @param parentId 父id,可为空
     * @return
     */
    <T> void update(T entity, IndexTypeEnum indexTypeEnum, String corpid, Long id, Long parentId) throws XbbException;

    /**
     * 删除
     * @param id 主体id
     * @param corpid 公司id
     * @param indexTypeEnum es索引
     * @param parentId 父id,可为空
     */
    void delete(Long id, String corpid, IndexTypeEnum indexTypeEnum, Long parentId) throws XbbException;

    /**
     * 物理删除，慎用！！！
     * @param physicalDeleteDTO 数据实体
     * @throws XbbException
     */
    void physicalDelete(PhysicalDeleteDTO physicalDeleteDTO) throws XbbException;

    /**
     * 物理删除，慎用！！！
     * @param id
     * @param corpid
     * @param indexTypeEnum
     * @param parentId
     * @throws XbbException
     */
    void physicalDelete(Long id, String corpid, IndexTypeEnum indexTypeEnum, Long parentId) throws XbbException;

    /**
     *通用表单（含data）的获取单个，其他的不可用
     * @param id
     * @param corpid
     * @param indexTypeEnum
     * @param fieldList
     * @return
     * @throws XbbException
     */
    PaasFormDataEntityExt getByKey(Long id, String corpid, IndexTypeEnum indexTypeEnum, List<String> fieldList) throws XbbException;

    /**
    * @Description:  通用表单（含data）的获取多个，其他的尽量不可用
    * @Param: [idIn, corpid, indexTypeEnum, fieldList]
    * @return: java.util.List<com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt>
    * @Author: cyy
    * @Date: 2019/12/9
    */
    List<PaasFormDataEntityExt> getByIdList(Collection<Long> idIn, String corpid, Integer saasMark, Integer businessType, Collection<String> fieldList, IndexTypeEnum indexTypeEnum) throws XbbException ;

    /**
     * 根据id获取数据
     * @param idIn
     * @param corpid
     * @param fieldList
     * @param indexTypeEnum
     * @param clazz
     * @return java.util.List<T>
     * @author 吴峰
     * @date 2022/5/23 10:02
     * @throws XbbException
     */
    <T> List<T> getByIdList(Collection<Long> idIn, String corpid, Collection<String> fieldList, IndexTypeEnum indexTypeEnum, Class<T> clazz) throws XbbException ;

    InsertDTO getInsertDTOForUpdateEntity(UpdateDataEntity jsonArray, IndexTypeEnum indexTypeEnum) ;

    /**
     * 根据条件
     * @author 吴峰
     * @date 2019/8/20 15:07
     * @param sourceBuilder 条件拼装
     * @param indexTypeEnum 索引
     * @param fieldList
     * @param page
     * @param pageSize
     * @return java.util.List<com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt>
     * @throws XbbException 业务异常
     * indexTypeEnum 核对完成
     */
    List<PaasFormDataEntityExt> list(SearchSourceBuilder sourceBuilder,IndexTypeEnum indexTypeEnum, List<String> fieldList, Integer page, Integer pageSize) throws XbbException;

    /**
     * 搜索
     * @param indexTypeEnum
     * @param businessType
     * @param sourceBuilder
     * @param fieldList
     * @param page
     * @param pageSize
     * @return
     * @throws XbbException
     * indexTypeEnum 核对完成
     */
    PaasFormDataEsListVO list(IndexTypeEnum indexTypeEnum, Integer businessType, SearchSourceBuilder sourceBuilder, List<String> fieldList,  Integer page, Integer pageSize) throws XbbException;

    /**
     * 搜索
     * @param indexTypeEnum
     * @param businessType
     * @param sourceBuilder
     * @param fieldList
     * @param page
     * @param pageSize
     * @param clazz 返回实体
     * @return
     * @throws XbbException
     * indexTypeEnum 核对完成
     */
    <T> QueryListVO<T> list(IndexTypeEnum indexTypeEnum, SearchSourceBuilder sourceBuilder, List<String> fieldList, Integer page, Integer pageSize, Class clazz, String sortField, SortOrder order) throws XbbException;

    /**
     *
     * @param updateDataList
     * @param indexTypeEnum
     * @throws XbbException
     * indexTypeEnum 核对完成
     */
    void updateBatchDataByType(List<UpdateStockDataEntity> updateDataList, IndexTypeEnum indexTypeEnum) throws XbbException;

    /**
     * 获取表单数据
     * @param boolQueryBuilder
     * @param indexTypeEnum
     * @param fieldList 查询的字段，例子["num_1", "text_1", "subForm_1.text_1"]
     * @param page
     * @param pageSize
     * @param clazz
     * @param sortField 排序字段
     * @see SortOrder
     * @param order 排序规则
     * @return java.util.List<T>
     * @author 吴峰
     * @date 2020/10/10 15:13
     * @throws XbbException
     * indexTypeEnum 核对完成
     */
    <T> List<T> getDataList(BoolQueryBuilder boolQueryBuilder, IndexTypeEnum indexTypeEnum,
                               List<String> fieldList, Integer page, Integer pageSize, Class<T> clazz, String sortField, SortOrder order) throws XbbException;    /**

     * 获取表单数据
     * @param boolQueryBuilder
     * @param aggregationBuilder
     * @param indexTypeEnum
     * @param fieldList 查询的字段，例子["num_1", "text_1", "subForm_1.text_1"]
     * @param page
     * @param pageSize
     * @param clazz
     * @param sortField 排序字段
     * @see SortOrder
     * @param order 排序规则
     * @return XbbAggregatedPage<T>
     * @author 吴峰
     * @date 2020/10/10 15:13
     * @throws XbbException
     * indexTypeEnum 核对完成
     */
    <T> XbbAggregatedPage<T> getDataAndAggregation(BoolQueryBuilder boolQueryBuilder, AggregationBuilder aggregationBuilder, IndexTypeEnum indexTypeEnum,
                                                   List<String> fieldList, Integer page, Integer pageSize, Class<T> clazz, String sortField, SortOrder order) throws XbbException;
    /**
     *获取统计
     * @param boolQueryBuilder
     * @param indexTypeEnum
     * @return java.lang.Integer
     * @author 吴峰
     * @date 2020/11/2 9:29
     *  indexTypeEnum 核对完成
     * @throws XbbException
     */
    long count(BoolQueryBuilder boolQueryBuilder, IndexTypeEnum indexTypeEnum) throws XbbException;

    /**
     * 根据data里某个条件搜索
     *
     * @param businessType 业务类型
     * @param attr   data里属性
     * @param value  data里属性的值
     * @param param 参数
     * @author 吴峰
     * @return java.util.List<com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt>
     * @date 2020/10/26 9:04
     * @throws XbbException
     */
    List<PaasFormDataEntityExt> selectByOneCondition(Integer businessType, String attr, Object value, Map<String,Object> param, IndexTypeEnum indexTypeEnum) throws XbbException;
    /**
     * 根据data里某个条件搜索
     *
     * @param businessType 业务类型
     * @param dataParam   data里属性
     * @param param 参数
     * @author 吴峰
     * @return java.util.List<com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt>
     * @date 2020/10/26 9:04
     * @throws XbbException
     */
    List<PaasFormDataEntityExt> selectByCondition(Integer businessType,Map<String, Object> dataParam, Map<String,Object> param, IndexTypeEnum indexTypeEnum) throws XbbException ;
    
    void updateByMarketIdAndDel(List<Long> idList,String corpid, IndexTypeEnum indexTypeEnum,Integer del) throws XbbException;

    /**
     * 根据id获取数据
     * @param ids
     * @param indexTypeEnum
     * @param formId
     * @param corpid
     * @param clazz
     * @param fieldList
     * @param sortField
     * @param order
     * @return java.util.List<T>
     * @author 吴峰
     * @date 2022/11/11 16:53
     * @throws XbbException
     */
    <T> List<T> getByDataIdList(List<Long> ids,IndexTypeEnum indexTypeEnum, Long formId, String corpid, Class<T> clazz, List<String> fieldList, String sortField, SortOrder order) throws XbbException;

    /**
     * 根据id查询主数据和子表单数据
     * @param corpid
     * @param saasMark
     * @param businessType
     * @param formId
     * @param dataIdList
     * @return java.util.List<com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt>
     * @author 吴峰
     * @date 2020/10/14 10:22
     * @throws XbbException
     */
    List<PaasFormDataEntityExt> dataListAndSubDataList(String corpid, Integer saasMark, Integer businessType,
                                                       Long formId, List<Long> dataIdList, IndexTypeEnum indexTypeEnum) throws XbbException;
    /**
     * @param list
     * @param corpid
     * @param saasMark
     * @param businessType
     * @param formId
     * @return void
     * @author 吴峰
     * @date 2020/10/15 11:35
     * @throws XbbException
     */
    void setSubFormData(List<PaasFormDataEntityExt> list, String corpid, Integer saasMark, Integer businessType, Long formId, IndexTypeEnum indexTypeEnum) throws XbbException;

}
